<template>
  <view class="tab-container" :key="Date.now()">
    <view class="tab-item" v-for="(item, index) in tabList" :key="index" @click="handleTabClick(index)">
      <image :src="isActive(index) ? item.activeIcon : item.icon" mode="scaleToFill" class="tab-item-image" />
      <text class="tab-item-text" :class="{ active: isActive(index) }">{{ item.text }}</text>
    </view>

  </view>
</template>

<script setup>
import { ref, computed,onMounted} from 'vue'
import { useUserStore } from '../../store/modules/useUserStore'
import { useTabBarStore } from '../../store/modules/useTabBarStore'
import tabConfig from '/static/config/tabBar.json'

const tabData = tabConfig;
const userStore = useUserStore()
const tabBarStore = useTabBarStore()


onMounted(() => {
  tabBarStore.setActiveIndex(0)
})



// 当前用户角色（响应式）
const role = computed(() => userStore.getRole) // 默认为 older

// 获取当前角色的 tabs 配置
const tabList = computed(() => {
  return tabData[role.value]?.tabs || []
})

// 获取当前角色的样式配置
const curStyle = computed(() => {
  return tabData[role.value]?.style || {}
})

// 当前激活 tab 的索引
const activeIndex = tabBarStore.getActiveIndex


// 判断当前 tab 是否激活
const isActive = (index) => {
  // console.log('isActive:', index, activeIndex)
  return index === activeIndex
}




const handleTabClick = (index) => {
  if(index==activeIndex){
    return
  }
  tabBarStore.setActiveIndex(index)
  const path = '/pages' + tabList.value[index].page
  console.log('goto:', path)
  uni.navigateTo({ url: path })

}


</script>

<style scoped>
.tab-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100rpx;
  background-color: #f8f9fa;
  /* 更柔和的背景色 */
  display: flex;
  justify-content: space-around;
  /* 自动分配间距 */
  align-items: center;
  box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.06);
  z-index: 999;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  /* 可伸缩，保证均分空间 */
  min-width: 120rpx;
  /* 最小宽度，防止挤压变形 */
  padding: 0 10rpx;
  text-align: center;
}

.tab-item-image {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 8rpx;
}

.tab-item-text {
  font-size: 24rpx;
  color: #666;
}

.tab-item-text.active {
  color: v-bind('curStyle.activeColor');
  /* 或写死颜色如 #42b983 */
}

</style>